import { buildMessage, ValidateBy, ValidationArguments, ValidationOptions } from 'class-validator'


/**
 * 判断验证码是否是4位数
 */
export function IsMsgCode(validationOptions?: ValidationOptions & { length: number }): PropertyDecorator {
    const options = validationOptions
    return ValidateBy(
        {
            name: 'IsMsgCode',
            validator: {
                validate: (value, args): boolean => String(value).length === options.length,
                defaultMessage: buildMessage((eachPrefix, args?: ValidationArguments) => (args.object['code'] ?? '') + '不是一个正确的验证码'
                    , options),
            },
        },
        options,
    )
}